
#include "game_controller\headers\game_controller.hpp"
#include "game_controller\headers\game_controller_level_controller.hpp"

#include "character_controller\headers\character_controller.hpp"

#include "game_model\headers\game_model.hpp"
#include "game_model\ih\game_model_iobject_factory.hpp"

#include "tools\library_loader.hpp"

#include <QtCore\qplugin.h>
#include <QLibrary.h>

#include <assert.h>

namespace GameController {

class GameControllerPluginInstance
	:	public GameControllerPlugin
{

public:

	GameControllerPluginInstance()
		:	m_characterController( NULL )
		,	m_gameModel( NULL )
	{
		ensureGameModelPluginIsLoaded();

		m_levelController.reset( new LevelController( m_gameModel->getObjectFactory() ) );

		m_levelController->loadLevel( 1 );
	}

	~GameControllerPluginInstance()
	{
	}

public:

	virtual bool isEverythingCool() const
	{
		return true;
	}

public:

	virtual void handleNextTimeStep()
	{
	}

	virtual void handleUserActivity()
	{
		ensureCharacterControllerPluginIsLoaded();
	}

	virtual std::shared_ptr< IScene > getScene(
			std::shared_ptr< GameModel::IDistanceUnit > _from
		,	std::shared_ptr< GameModel::IDistanceUnit > _to )
	{
		return m_levelController->getCurrentLevel()->formScene( _from, _to );
	}

private:

	void ensureGameModelPluginIsLoaded()
	{
		if( !m_gameModel )
			m_gameModel = loadLibrary< GameModel::GameModelPlugin > ( "game_model", "getGameModelPlugin" );

		bool isEverytingCool = m_gameModel->isEverythingCool();
	}

	void ensureCharacterControllerPluginIsLoaded()
	{
		if( !m_characterController )
			m_characterController = loadLibrary< CharacterController::CharacterControllerPlugin > ( "character_controller", "getCharacterControllerPlugin" );

		bool isEverytingCool = m_characterController->isEverythingCool();
	}

private:

	std::shared_ptr< LevelController > m_levelController;

	CharacterController::CharacterControllerPlugin* m_characterController;

	GameModel::GameModelPlugin* m_gameModel;

};

Q_EXTERN_C GAME_CONTROLLER_EXPORT GameControllerPlugin* getGameControllerPlugin()
{
	return new GameControllerPluginInstance;
}

} // namespace GameController